package com.prs.crm.action.cqunicom;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.cqunicom.BargainDomain;
import com.prs.crm.domain.cqunicom.PoplarOrder;
import com.prs.crm.domain.cqunicom.Services;
import com.prs.crm.service.cqunicom.OrderService;
import com.prs.crm.service.cqunicom.ServicesService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2011-3-16 下午04:21:07
 * 简单说明  交易查询
 */

public class BargainAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;

	private PaginationSupport pagination;
	
	private PoplarOrder order;
	
	private List<Services> serList;
	
	private Services services;
	
	private ServicesService servicesService;

	private OrderService orderService;
	
	private String conditionss="";
	
	private String sortStyle="";
	
	private String bargainState="";
	
	private Integer serverids=0;
	
	private String dateSletct="";
	
	private Date startTimes=null;
	
	private Date endTimes=null;
	
	private String orderId="";
	
	private String cusName="";
	
	private String cusTel="";
	
	private Date orderTime=null;
	
	private BargainDomain  bdObject=new BargainDomain();
	
	// 显示统计
	// 时间
	private String starts = "";
	private String ends = "";
	private String teanTim="";
	
	@Action("listBargainInquery")
	public String listBargainInquery(){
		
		serList=this.getServicesService().getAllServices();
		SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (startTimes == null) {
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DAY_OF_MONTH, -7);
			Date _currt = c.getTime();
			starts = ddf.format(_currt);// 开始时间
		} else {
			starts = ddf.format(startTimes);
		}
		if (endTimes == null) {
			ends = ddf.format(new Date());// 结束时间
		} else {
			ends = ddf.format(endTimes);
		}
        if(orderTime==null){
        	teanTim=ddf.format(new Date());
        }else{
        	teanTim=ddf.format(orderTime);
        }
		
		
		
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(PoplarOrder.class);
		 //排序方式
		  if("时间".equals(sortStyle)){
			  criteria.addOrder(Order.asc("recordTime"));  
		  }
		  
		  if("组查询".equals(conditionss)){
			 
			if("所有".equals(bargainState)){
				
			} else{
				criteria.createAlias("businessOrder", "rr");	
				criteria.add(Restrictions.eq("rr.transationSatet", bargainState));
			} 
			
			Services  ser=this.getServicesService().get(serverids);  
			
			criteria.add(Restrictions.eq("services", ser));  
			if("派单时间".equals(dateSletct)){
			criteria.add(Restrictions.between("orderDate",startTimes,endTimes));	
			}
			else if("完成时间".equals(dateSletct)){
			criteria.add(Restrictions.between("compleDate",startTimes,endTimes));		
			}
		  }
		  else if("指定查询".equals(conditionss)){
			  criteria.add(Restrictions.eq("orderCode",orderId));
			  criteria.add(Restrictions.eq("customerName",cusName));
			  criteria.add(Restrictions.eq("customerTel", cusTel));
			  criteria.add(Restrictions.eq("orderDate",orderTime));
		  }
		
			
			criteria.add(Restrictions.eq("modleName", "派单"));
			pagination = this.getPaginationHelper().prepare(pagination);
			this.setPagination(this.getOrderService().listByPage(criteria,
					pagination));
		
		
		return SUCCESS;
	}

	@Action("listBargainCount")
	public String listBargainCount(){
		serList=this.getServicesService().getAllServices();
		SimpleDateFormat ddf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (startTimes == null) {
			Calendar c = Calendar.getInstance();
			c.add(Calendar.DAY_OF_MONTH, -7);
			Date _currt = c.getTime();
			starts = ddf.format(_currt);// 开始时间
		} else {
			starts = ddf.format(startTimes);
		}
		if (endTimes == null) {
			ends = ddf.format(new Date());// 结束时间
		} else {
			ends = ddf.format(endTimes);
		}
        
		
		
		
		
		serList=this.getServicesService().getAllServices();
		if("".equals(conditionss) || conditionss==null){
			DetachedCriteria criteria = this.getPaginationHelper().createQuery(PoplarOrder.class);
			criteria.add(Restrictions.eq("modleName", "派单"));
			pagination = this.getPaginationHelper().prepare(pagination);
			this.setPagination(this.getOrderService().listByPage(criteria,
					pagination));
			List<PoplarOrder>  alls=this.getOrderService().getDispatchOrder();
			//总成交量
			int cj=0;
			//总成交价
			double cjprice=0.0;
			//总结单量
			int jdNum=0;
			//成交率
			String cjlv="";
			for(PoplarOrder  po:alls){
				if(po.getBusinessOrder()!=null){
				if("已成交".equals(po.getBusinessOrder().getTransationSatet())){
					cj++;
					cjprice=cjprice+po.getBusinessOrder().getDealPrice();
				}
				}
				if("2".equals(po.getNewOrderState())){
					jdNum++;
				}
			}
			bdObject.setAllNum(cj+"");
			bdObject.setAllPrice(cjprice+"");
			if(jdNum==0){
				bdObject.setCjlv("0%");
			}else{
				bdObject.setCjlv((cj/jdNum*100)+"%");
			}
			
			bdObject.setReceiveNum(jdNum+"");
			
		}else{
		Services  ser=this.getServicesService().get(serverids); 
		
		
		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(PoplarOrder.class);
		 //排序方式
		  if("时间".equals(sortStyle)){
			  criteria.addOrder(Order.asc("recordTime"));  
		  }
		 criteria.createAlias("businessOrder", "xx");
		//时间选择
		if("预约时间".equals(conditionss)){
			
			criteria.add(Restrictions.between("xx.outDate",startTimes,endTimes));
		}
		else if("派单时间".equals(conditionss)){
			criteria.add(Restrictions.between("orderDate",startTimes,endTimes));	
		}
		else if("完成时间".equals(conditionss)){
			criteria.add(Restrictions.between("compleDate",startTimes,endTimes));		
		}
		if("所有".equals(bargainState)){
			
		}else{
			criteria.add(Restrictions.eq("xx.transationSatet",bargainState));
		}
		
		criteria.add(Restrictions.eq("services", ser)); 
		
		criteria.add(Restrictions.eq("modleName", "派单"));
		
	
		pagination = this.getPaginationHelper().prepare(pagination);
		
		this.setPagination(this.getOrderService().listByPage(criteria,
				pagination));
		}
		return SUCCESS;
	}

	@Action("listLitionInquery")
	public String listLitionInquery(){
		return SUCCESS;
	}
	
	public BargainDomain getBdObject() {
		return bdObject;
	}

	public void setBdObject(BargainDomain bdObject) {
		this.bdObject = bdObject;
	}

	public Integer getServerids() {
		return serverids;
	}

	public void setServerids(Integer serverids) {
		this.serverids = serverids;
	}

	public String getDateSletct() {
		return dateSletct;
	}

	public void setDateSletct(String dateSletct) {
		this.dateSletct = dateSletct;
	}

	public Date getStartTimes() {
		return startTimes;
	}

	public void setStartTimes(Date startTimes) {
		this.startTimes = startTimes;
	}

	public Date getEndTimes() {
		return endTimes;
	}

	public void setEndTimes(Date endTimes) {
		this.endTimes = endTimes;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public String getCusName() {
		return cusName;
	}

	public void setCusName(String cusName) {
		this.cusName = cusName;
	}

	public String getCusTel() {
		return cusTel;
	}

	public void setCusTel(String cusTel) {
		this.cusTel = cusTel;
	}

	public Date getOrderTime() {
		return orderTime;
	}

	public void setOrderTime(Date orderTime) {
		this.orderTime = orderTime;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public PoplarOrder getOrder() {
		return order;
	}
	public void setOrder(PoplarOrder order) {
		this.order = order;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public List<Services> getSerList() {
		return serList;
	}

	public void setSerList(List<Services> serList) {
		this.serList = serList;
	}

	public String getBargainState() {
		return bargainState;
	}

	public void setBargainState(String bargainState) {
		this.bargainState = bargainState;
	}


	public Services getServices() {
		return services;
	}

	public void setServices(Services services) {
		this.services = services;
	}

	public ServicesService getServicesService() {
		return servicesService;
	}

	public void setServicesService(ServicesService servicesService) {
		this.servicesService = servicesService;
	}

	public String getConditionss() {
		return conditionss;
	}

	public void setConditionss(String conditionss) {
		this.conditionss = conditionss;
	}

	public String getSortStyle() {
		return sortStyle;
	}

	public void setSortStyle(String sortStyle) {
		this.sortStyle = sortStyle;
	}

	public String getStarts() {
		return starts;
	}

	public void setStarts(String starts) {
		this.starts = starts;
	}

	public String getEnds() {
		return ends;
	}

	public void setEnds(String ends) {
		this.ends = ends;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public String getTeanTim() {
		return teanTim;
	}

	public void setTeanTim(String teanTim) {
		this.teanTim = teanTim;
	}

}
